#!/usr/bin/python
# -*- coding: utf8 -*-

import sys
import os
import gc

from rule import *
from grammar import *

corpus = []

class Sentence:
    def __init__(self):
        self.words = None
        self.types = None
        self.cky = None

    def parse_corpus(self, txt, parser):
        cells = txt.split()
        
        self.words = []
        self.types = []
        for c in cells:
            w, t = c.strip().rsplit(u'/', 1)
            self.words.append(w)
            self.types.append(make_tuple(t))

        parser.parse(self.types)
        # print parser.debug_string(self.words).encode('utf8')

        # self.grammar = parser.build_grammar_tree()
        # print self.grammar.debug_string(self.words).encode('utf8')
        
        line = u'#' + u' '.join(self.words)
        # print line.encode('utf8')


def parse_corpus_file(filename):
    file = open(filename, 'r')
    lines = unicode(file.read(), 'gb18030').split('\n')

    parser = CKYParser()
    
    count = 0
    for line in lines:
        if line == "":
            return
        sentence = Sentence()  
        sentence.parse_corpus(line, parser)
        corpus.append(sentence)

        count += 1
        print count


    return None

if __name__ == '__main__':
    gc.set_debug(gc.DEBUG_LEAK)
    parse_rules(sys.argv[1])
    parse_corpus_file(sys.argv[2])

    #for c in corpus:
    #    line = u'#' + u' '.join(c.words)
    #    print line.encode('utf8')
